const Mock = require('mockjs')
const { artistInfo } = require('./index'); 

let Result = {
  code: 200,
  msg: 'success',
  data: null
}
/**
 * Agent section begin
 */
//agent list
let AgentList = [
  {
    "id": 1,
    "name": "陈晓琳",
    "address": "上海",
    "artists": [1, 3, 5] // 新增字段，存储艺人ID
  },
  {
    "id": 2,
    "name": "王志远",
    "address": "北京",
    "artists": [2, 4]
  },
  {
    "id": 3,
    "name": "林思涵",
    "address": "高雄",
    "artists": [7]
  },
  {
    "id": 4,
    "name": "杨振宇",
    "address": "成都",
    "artists": [8, 9]
  },
  {
    "id": 5,
    "name": "周雅文",
    "address": "香港",
    "artists": [6, 10]
  },
  {
    "id": 6,
    "name": "郑浩天",
    "address": "深圳",
    "artists": [11, 12]
  },
  {
    "id": 7,
    "name": "吴嘉怡",
    "address": "澳门",
    "artists": [13]
  },
  {
    "id": 8,
    "name": "张天翊",
    "address": "厦门",
    "artists": [14]
  },
  {
    "id": 9,
    "name": "黄薇安",
    "address": "广州",
    "artists": [15]
  },
  {
    "id": 10,
    "name": "刘建宏",
    "address": "杭州",
    "artists": [1, 3]
  }
];

/**
 * agent function
 */
//show agents' information

/*
Mock.mock('/getAgentInfo','get',()=>{
  Result.data=AgentList
  return Result
})

//add agent
Mock.mock('/addAgent','post',(options)=>{
  const body=JSON.parse(options.body);
  const newId=Math.max(...AgentList.map(agent=>agent.id))+1;
  const newAgent={
    id: newId,
    name: body.name,
    address: body.address || '',
    artists: body.artists || [] 
  }
  AgentList.push(newAgent);
  Result.msg='添加经纪人成功'
  return Result;
})

//modify agent
Mock.mock(new RegExp('/updateAgent/\\d+'), 'put', (options) => {
  const id = parseInt(options.url.match(/\/updateAgent\/(\d+)/)[1]);
  const body = JSON.parse(options.body);
  const index = AgentList.findIndex(agent => agent.id === id);
  if (index !== -1) {
    AgentList[index] = {
      ...AgentList[index],
      name: body.name,
      address: body.address || '',
      artists: body.artists || AgentList[index].artists // 更新artists字段
    };
    Result.msg = '修改经纪人成功';
  } else {
    Result.code = 404;
    Result.msg = '未找到该经纪人';
  }
  return Result;
});

//delete agent
Mock.mock(new RegExp('/deleteAgent/\\d+'), 'delete', (options) => {
  // 获取经纪人 ID
  const id = parseInt(options.url.match(/\/deleteAgent\/(\d+)/)[1]);
  
  // 查找经纪人
  const index = AgentList.findIndex(agent => agent.id === id);
  
  if (index !== -1) {
    // 删除经纪人
    AgentList.splice(index, 1);
    
    Result.msg = '删除经纪人成功';
  } else {
    Result.code = 404;
    Result.msg = '未找到该经纪人';
  }
  
  return Result;
});

//get responsible artist
Mock.mock(new RegExp('/getAgentResponsibleArtists/\\d+'), 'get', (options) => {
  const agentId = parseInt(options.url.match(/\/getAgentResponsibleArtists\/(\d+)/)[1]);
  const agent = AgentList.find(a => a.id === agentId);
  if (!agent) {
    Result.code = 404;
    Result.msg = '经纪人不存在';
    return Result;
  }
  
  // 从agent的artists数组中获取艺人ID，然后从艺人列表artistInfo中获取艺人对象
  const assignedArtists = artistInfo.filter(artist => 
    agent.artists.includes(artist.id)
  );
  
  Result.data = assignedArtists;
  return Result;
});

//assign artist
Mock.mock('/assignArtist', 'post', (options) => {
  const body = JSON.parse(options.body);
  const { agentId, artistId } = body;
  const agentIndex = AgentList.findIndex(a => a.id === agentId);
  
  if (agentIndex === -1) {
    Result.code = 404;
    Result.msg = '经纪人不存在';
    return Result;
  }
  
  // 检查艺人是否存在
  const artist = artistInfo.find(a => a.id === artistId);
  if (!artist) {
    Result.code = 404;
    Result.msg = '艺人不存在';
    return Result;
  }
  
  // 检查是否已经指派
  if (!AgentList[agentIndex].artists.includes(artistId)) {
    AgentList[agentIndex].artists.push(artistId);
    Result.msg = '指派成功';
  } else {
    Result.msg = '该艺人已指派给此经纪人';
  }
  
  return Result;
});

//delete assgin artist
Mock.mock('/unassignArtist', 'post', (options) => {
  const body = JSON.parse(options.body);
  const { agentId, artistId } = body;
  const agentIndex = AgentList.findIndex(a => a.id === agentId);
  
  if (agentIndex === -1) {
    Result.code = 404;
    Result.msg = '经纪人不存在';
    return Result;
  }
  
  // 从artists数组中移除
  const artistIndex = AgentList[agentIndex].artists.indexOf(artistId);
  if (artistIndex !== -1) {
    AgentList[agentIndex].artists.splice(artistIndex, 1);
    Result.msg = '移除成功';
  } else {
    Result.code = 404;
    Result.msg = '该艺人未指派给此经纪人';
  }
  
  return Result;
});
*/
/**
 * Agent section end
 */
